import Vue from 'vue'
import Router from 'vue-router'
import errorPage from '@/components/errorpage'
//搜索
import search from '@/components/share/search/search';
//语音回复
import feedback from '@/components/share/feedback/feedback.vue';
import { indexRoutes } from '@/components/index/router/index.routes'
import { userRoutes } from '@/components/user/router/user.routes'
import { orderRoutes } from '@/components/order/router/order.routes'
import { contactsRoutes } from '@/components/contacts/router/contacts.routes'
import { messageRoutes } from '@/components/message/router/msg.routes'
import { mailRoutes } from '@/components/mail/router/mail.routes'
import { indexManRoutes } from '@/components/index_man/router/indexMan.routes'
import { pcRoutes } from '@/components/pc/router/pc.routes'
import { loginRoutes } from '@/components/login/router/login.routes'
import { settleRoutes } from '@/components/settle/router/settle.routes'

import { dingPagesRoutes } from '@/components/DingPages/router/dingPages.routes'

import Axios from 'axios'

Vue.use(Router)

let userinfo = localStorage.userinfo ? JSON.parse(localStorage.userinfo):null;


const router = new Router({
  routes: [
    //路径重定向
    { path: '/', redirect: '/index' },
    ...indexRoutes,
    ...userRoutes,
    ...orderRoutes,
    ...contactsRoutes,
    ...messageRoutes,
    ...mailRoutes,
    ...indexManRoutes,
    ...loginRoutes,
    ...pcRoutes,
    ...settleRoutes,
    ...dingPagesRoutes,
    {
      path:'/search',
      name: '搜索',
      component: search
    },
    {
      path: '/feedback',
      name: '反馈',
      component: feedback
    },
    //默认错误地址跳转页
    {
      path: '*',
      name: 'errorPage',
      component: errorPage
    }
  ]
})

//路由权限跳转控制
router.beforeEach((to, from, next) => {
  let path = to.matched.filter(v => v.meta.requireAuth);
  
  //抓取访问记录
  if (userinfo == null) {
    userinfo = {
      Name: '未登录',
      LogonUser: '',
    }
  }
  let log = `登录用户：${userinfo.Name}；账号：${userinfo.LogonUser}；访问页面：${to.name}，路径：${location.protocol}//${location.host}${to.path}`,
    JLog = {
      UserName: userinfo.Name,
      LogonUser: userinfo.LogonUser,
      PageName: to.name,
      Path: `${location.protocol}//${location.host}${to.path}`,
      Content: log,
    };
  Axios.post(`${process.env.API_ROOT}/api/webchart/AccessRecording`, JLog);
  //抓取访问记录
  

  if (path.length) {
      // let metaName = path[0].meta.requireAuth,
      //     adminLogin = (metaName=="admin" && !cookie.getCookie('accountId')),
      //     manaLogin = (metaName=="manager" && !cookie.getCookie('managerId'));
      // if(adminLogin || manaLogin){
      //   next({
      //     path: '/login',
      //   })
      // }else{
      //   next();
      // }
  } else {
      next();
  }
})

export default router